package o;

import android.graphics.Bitmap;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import com.huawei.hwphy2d.physical.util.BarrierParameters;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import o.lz.a;
import o.lz.b;

public class bzt {
    @ColorInt
    public static int ˊ(@Nullable Bitmap bitmap, @Nullable String str) {
        if (bitmap == null) {
            return -1;
        }
        long nanoTime = System.nanoTime();
        a ˋ = lz.ˎ(bitmap).ˋ(4);
        ˋ.ॱ();
        ˋ.ˎ(new bzs());
        List ˎ = ˋ.ˎ().ˎ();
        bqx.ˏ("PaletteUtil", "getMajorColor consume time (in nanosecond): " + (System.nanoTime() - nanoTime));
        b ˋ2 = ˋ(ˎ, str);
        if (ˋ2 != null) {
            return ˋ2.ˏ();
        }
        return -1;
    }

    @Nullable
    static b ˋ(@Nullable List<b> list, @Nullable String str) {
        int i = 0;
        if (list == null || list.size() == 0) {
            return null;
        }
        if (list.size() == 1) {
            return (b) list.get(0);
        }
        List<b> arrayList = new ArrayList(list);
        Collections.sort(arrayList, new Comparator<b>() {
            public /* synthetic */ int compare(Object obj, Object obj2) {
                return ˎ((b) obj, (b) obj2);
            }

            public int ˎ(b bVar, b bVar2) {
                return bVar2.ˊ() - bVar.ˊ();
            }
        });
        for (b bVar : arrayList) {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("id=");
            stringBuilder.append(str);
            stringBuilder.append(";");
            stringBuilder.append("argb=");
            stringBuilder.append(String.format(Locale.ENGLISH, "%h", new Object[]{Integer.valueOf(bVar.ˏ())}));
            stringBuilder.append(";");
            stringBuilder.append("population=");
            stringBuilder.append(bVar.ˊ());
            bqx.ˏ("PaletteUtil", stringBuilder.toString());
        }
        for (b bVar2 : arrayList) {
            i = bVar2.ˊ() + i;
        }
        i = (int) (((float) i) * BarrierParameters.RESTITUTION);
        bzs o_bzs = new bzs();
        for (b bVar22 : arrayList) {
            if (o_bzs.ˊ(bVar22.ˏ(), bVar22.ॱ())) {
                return bVar22;
            }
            if (bVar22.ˊ() < i) {
                return null;
            }
        }
        return null;
    }
}
